<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      @submit.native.prevent>
      <el-form-item label="ID" prop="name">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">搜索
        </el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      stripe :data="commandList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{'background': '#F6F8FB'}"
      :height="maxHeight"
      v-scrollbar="'.el-table__body-wrapper'">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" sortable/>
      <el-table-column label="名称" align="center" prop="name" sortable/>
      <el-table-column label="URL" align="center" prop="url" :show-overflow-tooltip="true"/>
      <el-table-column label="参数" align="center" prop="pageParams" :show-overflow-tooltip="true"/>
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="权限角色" align="center" prop="roleIds"/>
      <el-table-column width="120" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div class="operate-group">
            <span
              style="cursor:pointer;color: #409eff;font-size:12px;"
              type="text"
              class="el-icon-edit"
              :plain=false
              @click="handleUpdate(scope.row)"
            >编辑</span>
            <span
              style="cursor:pointer;color: #409eff;font-size:12px;"
              type="text"
              class="el-icon-delete"
              :plain=false
              @click="handleDelete(scope.row)"
            >删除</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改command命令对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="50%"
      append-to-body
      v-dialogDrag
      :close-on-click-modal="false">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        @submit.native.prevent>
        <el-row>
          <el-col :span="24">
              <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="类型">
          <el-radio-group v-model="form.type">
            <el-radio label="1">按钮</el-radio>
            <el-radio label="2">跳链</el-radio>
            <el-radio label="3">ES搜索</el-radio>
            <el-radio label="4">文件上传</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作类型" prop="operateType" v-if="form.type=='1'">
              <el-select v-model="form.operateType" placeholder="请选择">
                <el-option key="created" label="创建" value="created"></el-option>
                <el-option key="modify" label="修改" value="modify"></el-option>
                <el-option key="remove" label="删除" value="remove"></el-option>
                <el-option key="release" label="发布" value="release"></el-option>
                <el-option key="revise" label="换版" value="revise"></el-option>
                <el-option key="obsolete" label="作废" value="obsolete"></el-option>
                <el-option key="views" label="浏览" value="views"></el-option>
                <el-option key="checkin" label="上传" value="checkin"></el-option>
                <el-option key="download" label="下载" value="download"></el-option>
                <el-option key="promote" label="升级" value="promote"></el-option>
                <el-option key="demote" label="降级" value="demote"></el-option>
                <el-option key="subordinate" label="查看下级" value="subordinate"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="上传类型" prop="operateType" v-if="form.type=='4'">
              <el-select v-model="form.operateType" placeholder="请选择">
                <el-option key="singleUpdate" label="单文件更新" value="singleUpdate"></el-option>
                <el-option key="multiUpdate" label="多文件更新" value="multiUpdate"></el-option>
                <el-option key="singleAdd" label="单文件上传" value="singleAdd"></el-option>
                <el-option key="multiAdd" label="多文件上传" value="multiAdd"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
              <el-form-item label="URL" prop="url">
          <el-input v-model="form.url" placeholder="请输入URL" />
        </el-form-item>
          </el-col>
        <el-col :span="24">
        <el-form-item label="参数" prop="pageParams">
          <el-input v-model="form.pageParams" type="textarea" placeholder="请输入参数" />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="显示排序" prop="sort">
          <el-input-number  v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="显示位置" prop="location">
          <el-select v-model="form.location" placeholder="请选择">
            <el-option key="1" label="行内" value="1"></el-option>
            <el-option key="2" label="顶部" value="2"></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图标类">
            <el-input v-model="form.icon" placeholder="请输入图标类" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="form.location=='2'?'编辑条件':'显示条件'">
            <el-input v-model="form.show" placeholder="请输入显示条件" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="样式类型" prop="styleType" v-if="form.type=='1'">
          <el-select v-model="form.styleType" placeholder="请选择">
            <el-option key="primary" label="蓝色" value="primary"></el-option>
            <el-option key="success" label="绿色" value="success"></el-option>
            <el-option key="danger" label="红色" value="danger"></el-option>
            <el-option key="info" label="灰色" value="info"></el-option>
            <el-option key="warning" label="黄色" value="warning"></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">正常</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        </el-col>
          <el-col :span="24">
            <el-form-item label="角色">
              <el-select style="width: 100%;" v-model="form.roleIds" multiple placeholder="请选择">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCommand,
    getCommand,
    delCommand,
    addCommand,
    updateCommand
  } from "@/api/pms/command";
  import IconSelect from "@/components/IconSelect";
  import {listRole} from "@/api/system/role";

  export default {
    inject: ['app'],
    name: "Command",
    components: { IconSelect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // command命令表格数据
        commandList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        closeOnClickModal: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
        },
        // 角色选项
        roleOptions: [],
      };
    },
    created() {
      this.getList();
    },
    computed: {
      maxHeight() {
        if (this.app.screenHeight) {
          return this.app.screenHeight - 250
        }
      }
    },
    methods: {
      // 选择图标
      selected(name) {
        this.form.icon = name;
      },
      /** 查询command命令列表 */
      getList() {
        this.loading = true;
        listCommand(this.queryParams).then(response => {
          this.commandList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          type:"1",
          status:"1",
          location:"1",
          sort:1,
          roleIds:[]
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        listRole({}).then(response => {
            this.roleOptions = response.rows;
        });
        this.open = true;
        this.title = "添加command命令";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        listRole({}).then(response => {
            this.roleOptions = response.rows;
        });
        const id = row.id || this.ids
        getCommand(id).then(response => {
          this.form = response.data;
          var roleids = response.data.roleIds;
          this.form.roleIds=[];
          if(roleids){
            this.roleOptions.map(item=>{
              if(roleids.indexOf(item.roleId)>=0){this.form.roleIds.push(item.roleId)}
            });
          }
          this.open = true;
          this.title = "修改command命令";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if(this.form.roleIds.length>0){
              this.form.roleIds=this.form.roleIds.join(",");
            }else{
              this.form.roleIds=null;
            }
            if (this.form.id != undefined) {
              updateCommand(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCommand(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除command命令编号为"'
                      + (Array.isArray(ids) && ids.length > 2
                        ? (ids[0] + '等')
                        : ids)
                      + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCommand(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      // 状态翻译
      statusFormat(row, column) {
        if (row.status == 1) {
          return "正常";
        }else{
          return "禁用";
        }
      }
    }
  };
</script>
